//
//  BVRegisterViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/13.
//  Copyright © 2016年 com.yinding. All rights reserved.
//  注册页面

#import "BVRegisterViewController.h"
#import "BVCaptchaButton.h"
#import "BVMineTool.h"
#import "NSString+BVValiMobile.h"

@interface BVRegisterViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet BVCaptchaButton *captchaBtn;
@property (nonatomic, strong) BVMineTool *mineTool;
@property (weak, nonatomic) IBOutlet UITextField *mobile;
@property (weak, nonatomic) IBOutlet UITextField *verifycode;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *confirmPassword;
@property (nonatomic) BOOL isTelValid;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation BVRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"快速注册"];
    [self.captchaBtn setEnabled:NO];
}

- (void)setCaptchaBtn:(BVCaptchaButton *)captchaBtn {
    _captchaBtn = captchaBtn;
    [captchaBtn setCountdown:60];
}

#pragma mark -
#pragma mark - 获取验证码
- (IBAction)getCaptchaClick:(id)sender {
    [self.mineTool fetchVerifyCode:self.mobile.text success:^(NSString *status) {
        [self showMessage:@"获取验证码成功"];
    } failure:^(NSError *error) {
        [self showError:error.description];
    }];
}

- (IBAction)registerClick:(id)sender {
    /* 注册规范：
     1. 密码长度不少于6位
     2. 密码中必须同时包含数字和字母
    */
    if (self.mobile.text.length == 0 || self.verifycode.text.length == 0 || self.password.text.length == 0 || self.confirmPassword.text.length == 0) {
        
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的注册信息不完整,请重新提交~" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:OKAction];
        [self presentViewController:controller animated:YES completion:nil];
    } else {
        if (self.verifycode.text.length != 6 || [self.verifycode.text isEqualToString:@""]) {
            [self showError:@"验证码无效"];
        } else {
            if (![NSString judgePassWord:self.password.text]) {
                [self showError:@"密码无效"];
            } else {
                if (![self.password.text isEqualToString:self.confirmPassword.text]) {
                    [self showError:@"输入密码不一致"];
                } else {
                    [self.mineTool registered:self.mobile.text verify_code:self.verifycode.text password:self.password.text success:^(NSMutableDictionary *dic) {
                        
                        if ([dic[@"code"] isEqualToString:@"0"]) {
                            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"注册成功，去登陆吧~" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                [self.navigationController popViewControllerAnimated:YES];
                            }];
                            [alert addAction:OK];
                            [self presentViewController:alert animated:YES completion:nil];

                        } else {
                            [self showMessage:dic[@"msg"]];
                        }
                    } failure:^(NSError *error) {
                        [self showMessage:@"注册失败"];
                    }];
                }
            }
        }
    }
}

- (IBAction)textFieldChanged:(UITextField *)textField {
    self.isTelValid = [NSString valiMobile:textField.text];
    [self.captchaBtn setEnabled:self.isTelValid];
}


- (BVMineTool *)mineTool {
    if (!_mineTool) {
        _mineTool = [[BVMineTool alloc] init];
    }
    return _mineTool;
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}


@end
